package com.dkm.dkmproject.action;


import com.dkm.dkmproject.domain.ServiceTb;
import com.dkm.dkmproject.domain.ServicetypeTb;
import com.dkm.dkmproject.service.IServiceTbService;
import com.dkm.dkmproject.utils.FileTools;
import com.dkm.dkmproject.utils.Message;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务表 前端控制器
 * </p>
 *
 * @author 周佳佳
 * @since 2020-04-26
 */
@Api(description="服务表接口文档")
@Controller
@ResponseBody
@RequestMapping("/dkm.dkmproject/serviceTb")
public class ServiceTbController {

    @Autowired
    private IServiceTbService serviceTbService;
    @Autowired
    private FileTools fileTools;

    @ApiOperation(value = "根据服务类型主键查询服务",notes = "返回服务的list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name="scbId",value = "服务主键"),
            @ApiImplicitParam(paramType = "path",dataType = "Integer",name="syId",value = "服务类型主键",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbName",value = "服务方名称"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbAddress",value = "服务方地址"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbArea",value = "服务区域"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbPhone",value = "联系电话"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbDetails",value = "服务详情"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name="scbStar",value = "服务星级"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbFile",value = "服务合同"),
            @ApiImplicitParam(paramType = "query",dataType = "BigDecimal",name="scbMoney",value = "服务费用"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp1",value = "拓展字段1"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp2",value = "拓展字段2"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp3",value = "拓展字段3")

    })
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
            @ApiResponse(code = 200, message = "返回成功")
    })
    @GetMapping("/findAll/{syId}")
    public   List<ServiceTb>  findAll(@PathVariable("syId") Integer syId){
        List<ServiceTb>  list=serviceTbService.findAll(syId);
        return list;
    }

    @ApiOperation(value = "增加服务的接口",notes = "成功返回成功，失败返回失败")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name="scbId",value = "服务主键"),
            @ApiImplicitParam(paramType = "path",dataType = "Integer",name="syId",value = "服务类型主键",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbName",value = "服务方名称",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbAddress",value = "服务方地址",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbArea",value = "服务区域",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbPhone",value = "联系电话",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbDetails",value = "服务详情",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name="scbStar",value = "服务星级",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbFile",value = "服务合同",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "BigDecimal",name="scbMoney",value = "服务费用",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp1",value = "拓展字段1"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp2",value = "拓展字段2"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp3",value = "拓展字段3")

    })
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
            @ApiResponse(code = 200, message = "返回成功")
    })
    @PostMapping("ServiceTb")
    public Message addServiceTb(@RequestBody  ServiceTb serviceTb){
        Message message=new Message();
        if(serviceTb.getFile1()!=null &&!"".equals(serviceTb.getFile1())){
            String path=fileTools.saveFile("order",serviceTb.getFile1());
            serviceTb.setScbFile(path);
        }
        int rows=serviceTbService.addService(serviceTb);
        if(rows>0){
            message.setNum(1);
            message.setMsg("增加成功");
        }else{
            message.setMsg("失败");
            message.setNum(0);
        }
        return message;
    }



    @ApiOperation(value = "根据服务主键的scbId查询服务详情",notes = "返回服务Json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name="scbId",value = "服务主键",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name="syId",value = "服务类型主键",required = true),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbName",value = "服务方名称"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbAddress",value = "服务方地址"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbArea",value = "服务区域"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbPhone",value = "联系电话"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbDetails",value = "服务详情"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name="scbStar",value = "服务星级"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="scbFile",value = "服务合同"),
            @ApiImplicitParam(paramType = "query",dataType = "BigDecimal",name="scbMoney",value = "服务费用"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp1",value = "拓展字段1"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp2",value = "拓展字段2"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name="exp3",value = "拓展字段3")
    })

    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
            @ApiResponse(code = 200, message = "返回成功")
    })

    @GetMapping("/findBId")
    public   ServiceTb findBId(@RequestParam(value = "scbId") Integer scbId){
        return  serviceTbService.selectByPrimaryKey(scbId);

    }
    @RequestMapping("selectAllTwo")
    @ResponseBody
    public Map<String,Object> selectAllTwo(@RequestParam("page") Integer pageNum , @RequestParam("limit") Integer pageSize){
        Map<String,Object> map=new HashMap<>();

        map.put("msg","");
        map.put("code",0);
        map.put("data",serviceTbService.selectAllTwo(pageNum,pageSize));
        map.put("count",serviceTbService.selectCountOne());
        return map;
    }
    @RequestMapping("addorUpdate")
    @ResponseBody
    public Message addorUpdate(ServiceTb serviceTb,Integer number){
        Message message=new Message();
        if(number==1){
            serviceTb.setExp5("1");
        }else if(number==2){
            serviceTb.setExp5("2");
        }
        if(serviceTb.getFileOne().getOriginalFilename()!=null &&!"".equals(serviceTb.getFileOne().getOriginalFilename())){
            String path = fileTools.saveFiletwo("service",serviceTb.getFileOne());
            serviceTb.setExp1(path);
        }
        if(serviceTb.getFileTwo().getOriginalFilename()!=null &&!"".equals(serviceTb.getFileTwo().getOriginalFilename())){
            String path = fileTools.saveFiletwo("service",serviceTb.getFileTwo());
            serviceTb.setExp3(path);
        }
        if(serviceTb.getFileThree().getOriginalFilename()!=null &&!"".equals(serviceTb.getFileThree().getOriginalFilename())){
            String path = fileTools.saveFiletwo("service",serviceTb.getFileThree());
            serviceTb.setExp6(path);
        }
        if(serviceTb.getFileFour().getOriginalFilename()!=null &&!"".equals(serviceTb.getFileFour().getOriginalFilename())){
            String path = fileTools.saveFiletwo("service",serviceTb.getFileFour());
            serviceTb.setExp7(path);
        }
        //编辑操作
        if(!StringUtils.isEmpty(serviceTb.getScbId())){
            serviceTbService.updateByPrimaryKeySelective(serviceTb);
        }else{
            serviceTbService.insertSelective(serviceTb);
        }
        message.setMsg("成功");
        message.setNum(1);
        return message;
    }
    @RequestMapping("findById")
    @ResponseBody
    public ServiceTb findById(Integer scbId){
        ServiceTb serviceTb=serviceTbService.selectByPrimaryKey(scbId);
        return serviceTb;
    }
    @PostMapping("/delete")
    public Message deleteByPrimaryKey(@RequestParam(value = "scbId") Integer scbId) {

        Message msg = new Message();
        int rows=serviceTbService.deleteTwo(scbId);
        if(rows>0){
            msg.setNum(1);
            msg.setMsg("操作成功!");
        }else{
            msg.setNum(0);
            msg.setMsg("操作失败");
        }

        return msg;
    }
}
